package yxy.game.pm2.command.hitplanegame;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HitPlaneMessage;
import yxy.game.pm2.module.hitplanegame.HitPlaneGameModule;


/****************************************
 * @name CMD_HitPlaneGoThrough
 * @description 打飞机通关记录
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.HitPlaneGoThrough, classtype = HitPlaneMessage.HitPlaneGoThroughRequest.class)
public class CMD_HitPlaneGoThrough extends Command<HitPlaneMessage.HitPlaneGoThroughRequest> {

    @Override
    public void execute(MessageSource messageSource, HitPlaneMessage.HitPlaneGoThroughRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        HitPlaneMessage.HitPlaneGoThroughResponse.Builder response = HitPlaneMessage.HitPlaneGoThroughResponse.newBuilder();
        try {
            HitPlaneGameModule module = HitPlaneGameModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            module.setCurrentId(module.getCurrentId() + 1);
            module.setTimes(module.getTimes() + 1);
            module.set();

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, HitPlaneMessage.HitPlaneGoThroughResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
